import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:dio/dio.dart';

import '../../core/offline/offline_mode_manager.dart';
import '../../core/connectivity/connectivity_service.dart';
import '../../core/sync/data_sync_service.dart';
import '../../data/database/app_database.dart';
import '../../data/datasources/remote/project_api_service.dart';
import '../../data/datasources/remote/auth_api_service.dart';
import '../../data/datasources/local/local_data_source.dart';
import '../../data/repositories/project_repository_impl.dart';

/// 离线模式状态Provider
final offlineModeStatusProvider = StreamProvider<OfflineModeStatus>((ref) {
  final manager = ref.watch(offlineModeManagerProvider);
  return manager.status;
});

/// 数据同步状态Provider
final dataSyncStatusProvider = StreamProvider<SyncStatus>((ref) {
  final syncService = ref.watch(dataSyncServiceProvider);
  return syncService.syncStatus;
});

/// 数据同步进度Provider
final dataSyncProgressProvider = StreamProvider<double>((ref) {
  final syncService = ref.watch(dataSyncServiceProvider);
  return syncService.syncProgress;
});

/// 网络连接状态Provider
final connectivityStatusProvider = StreamProvider<ConnectivityResult>((ref) {
  final connectivityService = ref.watch(connectivityServiceProvider);
  return connectivityService.connectionStatus;
});

/// OfflineModeManager Provider
final offlineModeManagerProvider = Provider<OfflineModeManager>((ref) {
  final manager = OfflineModeManager();
  // 确保只初始化一次
  ref.onDispose(() {
    manager.dispose();
  });
  return manager;
});

/// ConnectivityService Provider
final connectivityServiceProvider = Provider<ConnectivityService>((ref) {
  return ConnectivityService();
});

/// DataSyncService Provider
final dataSyncServiceProvider = Provider<DataSyncService>((ref) {
  return DataSyncService();
});

/// AppDatabase Provider
final appDatabaseProvider = Provider<AppDatabase>((ref) {
  return AppDatabase();
});

/// LocalDataSource Provider
final localDataSourceProvider = Provider<LocalDataSource>((ref) {
  final database = ref.watch(appDatabaseProvider);
  return LocalDataSource(database);
});

/// ProjectApiService Provider
final projectApiServiceProvider = Provider<ProjectApiService>((ref) {
  // 创建一个Dio实例，实际项目中应该使用配置好的Dio实例
  final dio = Dio();
  return ProjectApiService(dio);
});

/// AuthApiService Provider
final authApiServiceProvider = Provider<AuthApiService>((ref) {
  // 创建一个Dio实例，实际项目中应该使用配置好的Dio实例
  final dio = Dio();
  return AuthApiService(dio);
});

/// ProjectRepositoryImpl Provider
final projectRepositoryImplProvider = Provider<ProjectRepositoryImpl>((ref) {
  final apiService = ref.watch(projectApiServiceProvider);
  final localDataSource = ref.watch(localDataSourceProvider);
  final offlineModeManager = ref.watch(offlineModeManagerProvider);
  return ProjectRepositoryImpl(apiService, localDataSource, offlineModeManager);
});

/// 强制离线模式状态
class ForceOfflineModeState {
  final bool isForcedOffline;

  ForceOfflineModeState(this.isForcedOffline);
}

/// 强制离线模式Notifier
class ForceOfflineModeNotifier extends Notifier<ForceOfflineModeState> {
  @override
  ForceOfflineModeState build() {
    return ForceOfflineModeState(false);
  }

  /// 设置强制离线模式
  void setForceOffline(bool value) {
    state = ForceOfflineModeState(value);
  }
}

/// 强制离线模式Provider
final forceOfflineModeProvider =
    NotifierProvider<ForceOfflineModeNotifier, ForceOfflineModeState>(
        ForceOfflineModeNotifier.new);

/// 切换离线模式的Notifier
class OfflineModeNotifier extends Notifier<OfflineModeStatus> {
  @override
  OfflineModeStatus build() {
    // 使用依赖注入获取OfflineModeManager实例
    final offlineModeManager = ref.read(offlineModeManagerProvider);

    // 监听离线模式状态变化
    offlineModeManager.status.listen((status) {
      state = status;
    });

    return OfflineModeStatus.online;
  }

  /// 强制切换到离线模式
  void forceOffline() {
    final offlineModeManager = ref.read(offlineModeManagerProvider);
    offlineModeManager.forceOfflineMode();
  }

  /// 强制切换到在线模式
  void forceOnline() {
    final offlineModeManager = ref.read(offlineModeManagerProvider);
    offlineModeManager.forceOnlineMode();
  }

  /// 手动触发数据同步
  Future<void> manualSync() async {
    final offlineModeManager = ref.read(offlineModeManagerProvider);
    await offlineModeManager.manualSync();
  }
}

/// 离线模式Notifier Provider
final offlineModeNotifierProvider =
    NotifierProvider<OfflineModeNotifier, OfflineModeStatus>(
        OfflineModeNotifier.new);
